import {Component, OnInit} from '@angular/core';
import {PageVoService} from '../../service/page-vo.service';
import {PageVo} from '../../entity/page-vo';
import {LinkDto} from '../../entity/link-dto';
import {SettingUtils} from '../../utils/setting-utils';
import {User} from '../../entity/user';
import {UserService} from '../../service/user.service';
import {DocFocusService} from '../../service/doc-focus.service';
import {BaseResponse} from '../../entity/base-response';
import {ArrayUtils} from '../../utils/array-utils';
import {MessageService} from '../../service/message.service';
import {AlertMessage} from '../../entity/alert-message';
import {TpStorageService} from '../../service/tp-storage.service';

@Component({
    selector: 'app-user-info',
    templateUrl: './user-info.component.html',
    styleUrls: ['./user-info.component.css']
})
export class UserInfoComponent implements OnInit {
    constructor(private pageVoService: PageVoService,
                private messageService: MessageService,
                private focusService: DocFocusService,
                private tpStorageService: TpStorageService,
                private userService: UserService) {
    }

    user: User;

    focusDocs: string[];

    ngOnInit() {
        // 加载用户数据
        this.user = this.userService.getUser();

        this.userService.getUserByToken().subscribe((resp: BaseResponse<User>) => {
            if (resp.code === 200 && resp.data) {
                this.user = resp.data;
                this.userService.setUser(this.user);
            }
        });

        this.focusDocs = this.focusService.getFocusDocIds();

        const pageVo: PageVo = this.pageVoService.getPageVo();
        pageVo.crumbs = SettingUtils.getSettingCrumbs(<LinkDto>{
            'text': '用户',
            frontIcon: 'person'
        });
        pageVo.leftMenus = SettingUtils.getSettingLeftNavs('user-info');
        pageVo.leftMenuId = 'setting';
        this.pageVoService.setPageVo(pageVo);
    }

    logout() {
        // 调出UC登出界面
        this.userService.logout();
    }

    /**
     * 删除某个文档的所有者
     * @param {string} docId
     */
    removeOwner(docId: string) {
        this.userService.removeOwner(docId).subscribe(result => {
            if (result) {
                // 删除成功
                this.messageService.add(<AlertMessage>{
                    id: 'remove-owner-' + docId,
                    type: 'success',
                    html: `放弃文档${docId}所有者身份成功！`,
                    time: 5
                });
            }
        });
    }

    clearLocalStorage() {
        this.tpStorageService.clearLocalStorage();
        // 重新加载用户信息
        this.userService.loadUserByToken();
    }
}
